<?php
namespace Admin\Controller;

use Admin\Controller\GlobalController;

class HomepageController extends GlobalController
{
    //首页广告图
    public function homelist()
    {
        $pageSize = 20;
        $where = array('is_deleted' => '0', 'type' => 'home');
        $homeModel = M('home_page');
        $count = $homeModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $homeModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //二维码处的广告图
    public function qrcodelist()
    {
        $pageSize = 20;
        $where = array('is_deleted' => '0', 'type' => 'qrcode');
        $homeModel = M('home_page');
        $count = $homeModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $homeModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //启动页广告图
    public function startlist()
    {
        $pageSize = 20;
        $where = array('is_deleted' => '0', 'type' => 'start');
        $homeModel = M('home_page');
        $count = $homeModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $homeModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //资讯的幻灯片
    public function newslist()
    {
        $pageSize = 20;
        $where = array('is_deleted' => '0', 'type' => 'news');
        $homeModel = M('home_page');
        $count = $homeModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $homeModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $newsModel = M('news');
        foreach ($list as $key => $row) {
            $newsInfo = $newsModel->where(array('news_id' => $row['related_id']))->find();
            $list[$key]['news_title'] = $newsInfo ? $newsInfo['news_title'] : '';
        }
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //资讯的幻灯片
    public function newslist_v2()
    {
        $pageSize = 20;
        $where = array('is_deleted' => '0', 'type' => 'news_v2');
        $homeModel = M('home_page');
        $count = $homeModel->where($where)->count();
        $pageObj = new \Admin\Util\Page($count, $pageSize);
        $list = $homeModel->where($where)->order('id desc')->limit($pageObj->firstRow . ',' . $pageObj->listRows)->select();
        $newsModel = M('news');
        foreach ($list as $key => $row) {
            $newsInfo = $newsModel->where(array('news_id' => $row['related_id']))->find();
            $list[$key]['news_title'] = $newsInfo ? $newsInfo['news_title'] : '';
        }
        $this->assign('list', $list);
        $this->assign('page', $pageObj->show());
        $this->display();
    }

    //添加或者修改
    public function save()
    {
        $homeModel = M('home_page');
        if (IS_POST) {
            $id = I('post.id', 0, 'intval');
            $data['type'] = I('post.type', '', 'trim');
            $data['img'] = I('post.img', '', 'trim');
            $data['expire_time'] = I('post.expire_time', 0, 'strtotime');
            $data['expire_time'] = $data['expire_time'] ? $data['expire_time'] + 86399 : 0;
            $data['related_id'] = I('post.related_id', 0, 'intval');
            if (empty($id)) {
                $homeModel->add($data);
            } else {
                $homeModel->where(array('id' => $id))->save($data);
            }
            $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
        }
        $id = I('get.id', 0, 'intval');
        $info = $homeModel->where(array('id' => $id))->find();
        $info['expire_time'] = $info['expire_time'] ? date('Y-m-d', $info['expire_time']) : '';
        $this->assign('type', I('get.type'));
        $this->assign('info', $info);
        $this->display();
    }

    public function del()
    {
        $id = I('post.id', 0, 'intval');
        $homeModel = M('home_page');
        $homeModel->where(array('id' => $id))->setField('is_deleted', '1');
        $this->ajaxReturn('', 'success', 'TIP_SUCCESS');
    }
}